{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Training\n",
    "\n",
    "We now run our RandomForest modeling software on our training set, described earlier, and derive a model along with some parameters describing how good our model is. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%pylab inline\n",
    "# We pull in the training, validation and test sets created according to the scheme described\n",
    "# in the data exploration lesson.\n",
    "\n",
    "import pandas as pd\n",
    "\n",
    "samtrain = pd.read_csv('../datasets/samsung/samtrain.csv')\n",
    "samval = pd.read_csv('../datasets/samsung/samval.csv')\n",
    "samtest = pd.read_csv('../datasets/samsung/samtest.csv')\n",
    "\n",
    "# We use the Python RandomForest package from the scikits.learn collection of algorithms. \n",
    "# The package is called sklearn.ensemble.RandomForestClassifier\n",
    "\n",
    "# For this we need to convert the target column ('activity') to integer values \n",
    "# because the Python RandomForest package requires that.  \n",
    "# In R it would have been a \"factor\" type and R would have used that for classification.\n",
    "\n",
    "# We map activity to an integer according to\n",
    "# laying = 1, sitting = 2, standing = 3, walk = 4, walkup = 5, walkdown = 6\n",
    "# Code is in supporting library randomforest.py\n",
    "\n",
    "import randomforests as rf\n",
    "samtrain = rf.remap_col(samtrain,'activity')\n",
    "samval = rf.remap_col(samval,'activity')\n",
    "samtest = rf.remap_col(samtest,'activity')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import sklearn.ensemble as sk\n",
    "rfc = sk.RandomForestClassifier(n_estimators=500, compute_importances=True, oob_score=True)\n",
    "train_data = samtrain[samtrain.columns[1:-2]]\n",
    "train_truth = samtrain['activity']\n",
    "model = rfc.fit(train_data, train_truth)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.97946768060836498"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# use the OOB (out of band) score which is an estimate of accuracy of our model.\n",
    "rfc.oob_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(0.052194982088894198, 'tAccMean'),\n",
       " (0.046418448022626055, 'tAccStd'),\n",
       " (0.043291948466911298, 'tJerkMean'),\n",
       " (0.053130159100753124, 'tGyroJerkMagSD'),\n",
       " (0.059232069484007693, 'fAccMean'),\n",
       " (0.048256742613275803, 'fJerkSD'),\n",
       " (0.13799007369608407, 'angleGyroJerkGravity'),\n",
       " (0.17036595812582825, 'angleXGravity'),\n",
       " (0.044817236984266123, 'angleYGravity')]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### TRY THIS\n",
    "# use \"feature importance\" scores to see what the top 10 important features are\n",
    "fi = enumerate(rfc.feature_importances_)\n",
    "cols = samtrain.columns\n",
    "[(value,cols[i]) for (i,value) in fi if value > 0.04]\n",
    "## Change the value 0.04 which we picked empirically to give us 10 variables\n",
    "## try running this code after changing the value up and down so you get more or less variables\n",
    "## do you see how this might be useful in refining the model?\n",
    "## Here is the code in case you mess up the line above\n",
    "## [(value,cols[i]) for (i,value) in fi if value > 0.04]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We use the predict() function using our model on our validation set and our test set and get the following results from our analysis of errors in the predictions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# pandas data frame adds a spurious unknown column in 0 position hence starting at col 1\n",
    "# not using subject column, activity ie target is in last columns hence -2 i.e dropping last 2 cols\n",
    "\n",
    "val_data = samval[samval.columns[1:-2]]\n",
    "val_truth = samval['activity']\n",
    "val_pred = rfc.predict(val_data)\n",
    "\n",
    "test_data = samtest[samtest.columns[1:-2]]\n",
    "test_truth = samtest['activity']\n",
    "test_pred = rfc.predict(test_data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "####Prediction Errors and Computed Error Measures  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean accuracy score for validation set = 0.846477\n",
      "mean accuracy score for test set = 0.895623\n"
     ]
    }
   ],
   "source": [
    "print(\"mean accuracy score for validation set = %f\" %(rfc.score(val_data, val_truth)))\n",
    "print(\"mean accuracy score for test set = %f\" %(rfc.score(test_data, test_truth)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# use the confusion matrix to see how observations were misclassified as other activities\n",
    "# See [5]\n",
    "import sklearn.metrics as skm\n",
    "test_cm = skm.confusion_matrix(test_truth,test_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# visualize the confusion matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPYAAADzCAYAAAC13+t7AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XtYVHX+B/D3AGPIReQiwwgoGLLDKMokgrmSmAKZYpqm\n4o1HtLayHhG7WFqKqWilrubPNt1ycyvEag1XgUfdHDLblQwp10uYMYaEKCAkchkYvr8/WE8MMMzh\nMJfD8Hk9z3keZubM+XzH8TPf7/dcPkfCGGMghNgUO2s3gBBiepTYhNggSmxCbBAlNiE2iBKbEBtE\niU2IDRJ1YtfV1SE+Ph79+/fHnDlzBG/n448/RlxcnAlbZj2nTp2CQqEQ9N4ff/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VRqodPp4O7ujsOHDyMxMREAkJiYiC+++ELw9imxCRFM+K1AmpubERYW\nBplMhgkTJmDYsGEmLT9sohsGEGIbunQRCGoNvPoVgFOtHm80uN3q6mrExcUhLS0Njz/+OG7fvs29\n5uHhgcrKSl7taavbe8WpAAPpvQwd73rwf8s9Gw1uwc3NDVOmTMF3333H3QHEx8eHyg8TYj11PBd9\n5eXl3B7vuro6HD9+HCqVCtOmTaPyw4RYn7AzVEpLS5GYmIjm5mY0Nzdj4cKFmDhxIlQqlcnKD3d7\njk1Ib9Qyxy7iuXagxaes1GMTIph4zymlxCZEMPGeU0qJTYhg1GMTYoPa7/EWC0psQgSjoTghNoiG\n4oTYIOqxCbFB1GMTYoOoxybEBlGPTYgNosNdhNgg6rEJsUE0xybEBom3x6ZCC4QI1sRzaS8nJwcK\nhQJDhw7Fli1bTN4ySmxCBBNWzFCn0+G5555DTk4OLl68iPT0dFy6dMmkLaPEJkQwYT12Xl4egoKC\nEBAQAKlUirlz5yIzM9OkLaPEJkQwYTXPSkpK4O/vzz328/NDSUmJSVtGO88IEWwdr7VcXFz0Hlui\nZDclNiECdKeGma+vL4qLi7nHxcXF8PPzM0WzODQUJ8TCwsPDceXKFWg0Gmi1WmRkZGDatGkmjUE9\nNiEW5uDggF27diEuLg46nQ5LlixBSEiISWNQ+WFCbBANxQmxQZTYhNggSmxCbBAlNiE2iBKbEBtE\niU2IDfp/RUzyzXdlcM0AAAAASUVORK5CYII=\n"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import pylab as pl\n",
    "pl.matshow(test_cm)\n",
    "pl.title('Confusion matrix for test data')\n",
    "pl.colorbar()\n",
    "pl.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# compute a number of other common measures of prediction goodness"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We now compute some commonly used measures of prediction \"goodness\".  \n",
    "For more detail on these measures see\n",
    "[6],[7],[8],[9]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy = 0.895623\n"
     ]
    }
   ],
   "source": [
    "# Accuracy\n",
    "print(\"Accuracy = %f\" %(skm.accuracy_score(test_truth,test_pred)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Precision = 0.897903\n"
     ]
    }
   ],
   "source": [
    "# Precision\n",
    "print(\"Precision = %f\" %(skm.precision_score(test_truth,test_pred)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Recall = 0.895623\n"
     ]
    }
   ],
   "source": [
    "# Recall\n",
    "print(\"Recall = %f\" %(skm.recall_score(test_truth,test_pred)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "F1 score = 0.896047\n"
     ]
    }
   ],
   "source": [
    "# F1 Score\n",
    "print(\"F1 score = %f\" %(skm.f1_score(test_truth,test_pred)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise\n",
    "\n",
    "Instead of using domain knowledge to reduce variables, use Random Forests directly on the full set of columns.  Then use variable importance and sort the variables.  \n",
    "\n",
    "Compare the model you get with the model you got from using domain knowledge.  \n",
    "You can short circuit the data cleanup process as well by simply renaming the variables x1, x2...xn, y where y is 'activity' the dependent variable.  \n",
    "\n",
    "Now look at the new Random Forest model you get.  It is likely to be more accurate at prediction than the one we have above. It is a black box model, where there is no meaning attached to the variables.  \n",
    "          \n",
    "* What insights does it give you?  \n",
    "* Which model do you prefer?  \n",
    "* Why?  \n",
    "* Is this an absolute preference or might it change?  \n",
    "* What might cause it to change? "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## References\n",
    "\n",
    "[1] Original dataset as R data https://spark-public.s3.amazonaws.com/dataanalysis/samsungData.rda  \n",
    "[2] Human Activity Recognition Using Smartphones http://archive.ics.uci.edu/ml/datasets/Human+Activity+Recognition+Using+Smartphones  \n",
    "[3] Android Developer Reference http://developer.android.com/reference/android/hardware/Sensor.html  \n",
    "[4] Random Forests http://en.wikipedia.org/wiki/Random_forest  \n",
    "[5] Confusion matrix http://en.wikipedia.org/wiki/Confusion_matrix\n",
    "[6] Mean Accuracy http://ieeexplore.ieee.org/xpl/login.jsp?tp=&arnumber=1054102&url=http%3A%2F%2Fieeexplore.ieee.org%2Fxpls%2Fabs_all.jsp%3Farnumber%3D1054102\n",
    "\n",
    "[7] Precision http://en.wikipedia.org/wiki/Precision_and_recall\n",
    "[8] Recall http://en.wikipedia.org/wiki/Precision_and_recall\n",
    "[9] F Measure http://en.wikipedia.org/wiki/Precision_and_recall"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>\n",
       "    @font-face {\n",
       "        font-family: \"Computer Modern\";\n",
       "        src: url('http://mirrors.ctan.org/fonts/cm-unicode/fonts/otf/cmunss.otf');\n",
       "    }\n",
       "    div.cell{\n",
       "        width:800px;\n",
       "        margin-left:auto;\n",
       "        margin-right:auto;\n",
       "    }\n",
       "    h1 {\n",
       "        font-family: \"Charis SIL\", Palatino, serif;\n",
       "    }\n",
       "    h4{\n",
       "        margin-top:12px;\n",
       "        margin-bottom: 3px;\n",
       "       }\n",
       "    div.text_cell_render{\n",
       "        font-family: Computer Modern, \"Helvetica Neue\", Arial, Helvetica, Geneva, sans-serif;\n",
       "        line-height: 145%;\n",
       "        font-size: 120%;\n",
       "        width:800px;\n",
       "        margin-left:auto;\n",
       "        margin-right:auto;\n",
       "    }\n",
       "    .CodeMirror{\n",
       "            font-family: \"Source Code Pro\", source-code-pro,Consolas, monospace;\n",
       "    }\n",
       "    .prompt{\n",
       "        display: None;\n",
       "    }\n",
       "    .text_cell_render h5 {\n",
       "        font-weight: 300;\n",
       "        font-size: 16pt;\n",
       "        color: #4057A1;\n",
       "        font-style: italic;\n",
       "        margin-bottom: .5em;\n",
       "        margin-top: 0.5em;\n",
       "        display: block;\n",
       "    }\n",
       "    \n",
       "    .warning{\n",
       "        color: rgb( 240, 20, 20 )\n",
       "        }\n",
       "</style>\n",
       "<script>\n",
       "    MathJax.Hub.Config({\n",
       "                        TeX: {\n",
       "                           extensions: [\"AMSmath.js\"]\n",
       "                           },\n",
       "                tex2jax: {\n",
       "                    inlineMath: [ ['$','$'], [\"\\\\(\",\"\\\\)\"] ],\n",
       "                    displayMath: [ ['$$','$$'], [\"\\\\[\",\"\\\\]\"] ]\n",
       "                },\n",
       "                displayAlign: 'center', // Change this to 'center' to center equations.\n",
       "                \"HTML-CSS\": {\n",
       "                    styles: {'.MathJax_Display': {\"margin\": 4}}\n",
       "                }\n",
       "        });\n",
       "</script>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML at 0x10cb56950>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from IPython.core.display import HTML\n",
    "def css_styling():\n",
    "    styles = open(\"../styles/custom.css\", \"r\").read()\n",
    "    return HTML(styles)\n",
    "css_styling()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
